package com.bestcem.xm.user.grpc.service.converter;

import com.bestcem.bp.xm.user.grpc.v1.services.GroupLevel;
import com.bestcem.bp.xm.user.grpc.v1.services.OpenGroupLevel;
import com.bestcem.xm.user.service.dto.group.UserGroupLevelDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 将本地处理结果转换成grpc请求响应参数
 *
 * @author panyun <yun.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/1/25 10:59
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserGroupLevelConverter {

    public static GroupLevel dtoToGroupLevel(UserGroupLevelDTO dto) {
        GroupLevel.Builder builder = GroupLevel.newBuilder();
        if (StringUtils.isNotBlank(dto.getGroupLevelId())) {
            builder.setId(dto.getGroupLevelId());
        }
        if (StringUtils.isNotBlank(dto.getOrgId())) {
            builder.setOrgId(dto.getOrgId());
        }
        if (StringUtils.isNotBlank(dto.getGroupLevelName())) {
            builder.setName(dto.getGroupLevelName());
        }
        if (Objects.nonNull(dto.getLevelNum())) {
            builder.setLevel(dto.getLevelNum());
        }

        builder.setDisplay(Objects.nonNull(dto.getLevelDisplay()) ? dto.getLevelDisplay() : "");
        if (StringUtils.isNotEmpty(dto.getLevelChildrenId())) {
            builder.setChildrenId(dto.getLevelChildrenId());
        }
        return builder.build();
    }

    /**
     * 将本地处理结果转换成grpc请求响应参数：API获取某个租户下的层级信息
     *
     * @param dtos 层级信息列表
     * @return com.bestcem.bp.xm.user.grpc.v1.services.OpenGroupLevel
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/02/17 17:48
     */
    public static List<OpenGroupLevel> dtosToOpenGroupLevelInfo(List<UserGroupLevelDTO> dtos) {
        List<OpenGroupLevel> grpcList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dtos)) {
            for (UserGroupLevelDTO dto : dtos) {
                OpenGroupLevel.Builder builder = OpenGroupLevel.newBuilder();
                if (StringUtils.isNotBlank(dto.getGroupLevelId())) {
                    builder.setId(dto.getGroupLevelId());
                }
                if (StringUtils.isNotBlank(dto.getGroupLevelName())) {
                    builder.setName(dto.getGroupLevelName());
                }
                if (Objects.nonNull(dto.getLevelNum())) {
                    builder.setLevel(dto.getLevelNum());
                }
                grpcList.add(builder.build());
            }
        }
        return grpcList;
    }

}
